package com.alcatellucent.ldap;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;

import com.alcatellucent.R;
import com.alcatellucent.app.LDAPApp;
import com.alcatellucent.vo.SearchVO;
import com.unboundid.ldap.sdk.LDAPConnection;
import com.unboundid.ldap.sdk.LDAPSearchException;
import com.unboundid.ldap.sdk.SearchResult;
import com.unboundid.ldap.sdk.SearchResultEntry;
import com.unboundid.ldap.sdk.SearchScope;

public class SearchActivity extends ListActivity {
	
	private LDAPConnection connection;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.searchscreen);
	}
	
	public void onClick(View v) {
		
		Log.d("TEST","onclick");
		
		connection = ((LDAPApp) getApplicationContext()).getConnection();
		System.out.println("Connectio not null");
		SearchResult searchResults ;
		try {
			System.out.println("Before serach");
			searchResults = connection.search("o=deviceDirectory",
			        SearchScope.SUB, "(&(cn=134799975*)(objectclass=device))");
			if(searchResults!=null) {
				System.out.println("serach result not null");
				String mail = null;
				List<SearchVO> resultlist = new ArrayList<SearchVO>();
				long count = searchResults.getEntryCount();
				if (searchResults.getEntryCount() > 0) {
					System.out.println("Entry count > 0");
					SearchVO searchVO;
					for(int i=0;i<count;i++) {
						System.out.println("Count value ="+i);
						SearchResultEntry entry = searchResults.getSearchEntries().get(i);
						if(entry!=null) {
							System.out.println("Entry not null for "+i);
							 searchVO = new SearchVO();
							 searchVO.setCn(entry.getAttributeValue("cn"));
							 System.out.println(" "+ entry.getAttributeValue("cn") );
							 searchVO.setDeviceid(entry.getAttributeValue("deviceid"));
							 System.out.println(" " + entry.getAttributeValue("deviceid"));
							 searchVO.setDevicetype(entry.getAttributeValue("devicetype"));
							 System.out.println(" " +entry.getAttributeValue("devicetype"));
							 searchVO.setDirectorynumber(entry.getAttributeValue("directorynumber"));
							 System.out.println(" " +entry.getAttributeValue("directorynumber"));
							 searchVO.setDisplayname(entry.getAttributeValue("displayname"));
							 System.out.println(" " +entry.getAttributeValue("displayname"));
							 searchVO.setNodetype(entry.getAttributeValue("nodetype"));
							 System.out.println(" " +entry.getAttributeValue("nodetype"));
							 
							 resultlist.add(searchVO);
							 System.out.println("Result list added");
						}
					}
					
					if(resultlist!=null) {
						ArrayAdapter<SearchVO> adapter = new ArrayAdapter<SearchVO>(getBaseContext(), 
					            android.R.layout.simple_list_item_1, resultlist);
						setListAdapter(adapter);
						adapter.notifyDataSetChanged();
						Log.d("LDAP", " Search Success");
					}
				}
			}
		} catch (LDAPSearchException e) {
			e.printStackTrace();
		}

		//connection.close();
		
	
		
		
		//new AsyncLDAP().execute();
		
		
		
	}

	/*
	private class AsyncLDAP extends AsyncTask<String, Void, Object> {
		
		@Override
		protected Object doInBackground(String... params) {
			
			List<SearchVO> resultlist = null;
			connection = ((LDAPApp)getApplicationContext()).getConnection();
			if(connection!=null) {
				System.out.println("Connectio not null");
				Log.d("LDAP","Connection not null");
				SearchResult searchResults ;
				try {
					System.out.println("Before serach");
					Log.d("LDAP","Before serach");
					searchResults = connection.search("o=deviceDirectory",
					        SearchScope.SUB, "(&(cn=134799975*)(objectclass=device))");
					if(searchResults!=null) {
						System.out.println("serach result not null");
						Log.d("LDAP","erach result not null");
						String mail = null;
						resultlist = new ArrayList<SearchVO>();
						long count = searchResults.getEntryCount();
						if (searchResults.getEntryCount() > 0) {
							System.out.println("Entry count > 0");
							Log.d("LDAP","Entry count > 0");
							SearchVO searchVO;
							for(int i=0;i<count;i++) {
								System.out.println("Count value ="+i);
								Log.d("LDAP","Count value ="+i);
								SearchResultEntry entry = searchResults.getSearchEntries().get(i);
								if(entry!=null) {
									System.out.println("Entry not null for "+i);
									Log.d("LDAP","Entry not null for "+i);
									 searchVO = new SearchVO();
									 searchVO.setCn(entry.getAttributeValue("cn"));
									 System.out.println(" "+ entry.getAttributeValue("cn") );
									 Log.d("LDAP"," "+ entry.getAttributeValue("cn") );
									 searchVO.setDeviceid(entry.getAttributeValue("deviceid"));
									 System.out.println(" " + entry.getAttributeValue("deviceid"));
									 Log.d("LDAP"," " + entry.getAttributeValue("deviceid"));
									 searchVO.setDevicetype(entry.getAttributeValue("devicetype"));
									 System.out.println(" " +entry.getAttributeValue("devicetype"));
									 Log.d("LDAP"," " +entry.getAttributeValue("devicetype"));
									 searchVO.setDirectorynumber(entry.getAttributeValue("directorynumber"));
									 System.out.println(" " +entry.getAttributeValue("directorynumber"));
									 Log.d("LDAP"," " +entry.getAttributeValue("directorynumber"));
									 searchVO.setDisplayname(entry.getAttributeValue("displayname"));
									 System.out.println(" " +entry.getAttributeValue("displayname"));
									 Log.d("LDAP"," " +entry.getAttributeValue("displayname"));
									 searchVO.setNodetype(entry.getAttributeValue("nodetype"));
									 System.out.println(" " +entry.getAttributeValue("nodetype"));
									 Log.d("LDAP"," " +entry.getAttributeValue("nodetype"));
									 resultlist.add(searchVO);
									 System.out.println("Result list added");
									 Log.d("LDAP","Result list added");
								}
							}
						}
					}
				} catch (LDAPSearchException e) {
					e.printStackTrace();
				}
			}
			return resultlist;
		}
		
		@Override
		protected void onPostExecute(Object result) {
			super.onPostExecute(result);
			System.out.println("on Post Execute ");
			if(result!=null) {
				System.out.println("Post Execute result not null");
				ArrayList<SearchVO> resultlist = (ArrayList<SearchVO>) (result);
				ArrayAdapter<SearchVO> adapter = new ArrayAdapter<SearchVO>(getBaseContext(), 
			            android.R.layout.simple_list_item_1, resultlist);
				setListAdapter(adapter);
				adapter.notifyDataSetChanged();
				Log.d("LDAP", " Search Success");
			}
		}
	}*/

		
}
